#####################
### Load Packages ###
#####################

library(haven)
library(dataverse)

# Set this Working Directory to the Folder with the Replication Files
setwd("C:/Steven/GDrive/Research Files/Clark-Rogers/Descriptive Representation/APSR Final Submission/Replication")


###
# Create Empty folders to store Figures, Tables, and intermediary datasets, if not already created
###

dir.create(file.path("Data/Formatted Data")) 
dir.create(file.path("Tables")) 
dir.create(file.path("Figures")) 
dir.create(file.path("Figures/Appendix")) 
dir.create(file.path("Scripts/Figures/Estimates")) 
dir.create(file.path("Scripts/Figures/Estimates/Racial Resentment")) 

###
# The following imports the 2018 CES, SLU CES Module, and Stauffer (2021) Replication Files directly from Harvard Dataverse
###

# Download SLU Module from Dataverse
SLUModule <- get_dataframe_by_name(
  filename = "CCES18_SLU_OUTPUT_vv.tab",
  dataset = "10.7910/DVN/JYNAQR", 
  server = "dataverse.harvard.edu")

# Download CES Common Content
CES <- get_dataframe_by_name(
  filename = "cces18_common_vv.dta",
  dataset = "10.7910/DVN/ZSBZ7K",
  .f = haven::read_dta,
  server = "dataverse.harvard.edu")

# Download Stauffer Congress Data
StaufferCongress <- get_dataframe_by_name(
  filename = "1618_CCES.tab",
  dataset = "10.7910/DVN/QYZGEI", 
  server = "dataverse.harvard.edu")

# Download Stauffer State Legislative Data
StaufferStateLeg <- get_dataframe_by_name(
  filename = "17_CCES.tab",
  dataset = "10.7910/DVN/QYZGEI", 
  server = "dataverse.harvard.edu")

###
# Import Local Variables to be later appended to Main Dataset
###
  
  StateLevelVariables <- read.csv("Data/StateLevelVariables.csv")
  MoCRace <- read.csv("Data/Congress-Minority-Legislators-Race-Gender.csv")
  DistancetoCapital <- read.csv("Data/DistancetoStateCapital.csv")
    DistancetoCapital$LogDistancefromCapital <- log(DistancetoCapital$Distance) # Log the Distance Variable
    DistancetoCapital$Distance <- NULL # Clean up the no longer needed raw distance measure
  StateTranslate <- cbind(state.name, state.abb)
  
###
# The following creates a "reduced" version of the 2018 CES using the most recent version of the 2018 CES
###
  
  nrow(CES)
  CES <- merge(CES, StateLevelVariables, by.x="inputstate", by.y="FIPS", all.x=TRUE)
  nrow(CES)
  
  ReducedCES <- data.frame(matrix(NA, nrow(CES), 0))
    ReducedCES$case_id <- CES$caseid
    ReducedCES$State <- CES$State
    ReducedCES$ZipCode <- CES$lookupzip
    ReducedCES$ZipCode[nchar(ReducedCES$ZipCode )<5] <-  paste("0", ReducedCES$ZipCode[nchar(ReducedCES$ZipCode )<5], sep="")
    ReducedCES$ZipCode[nchar(ReducedCES$ZipCode )<5] <-  paste("0", ReducedCES$ZipCode[nchar(ReducedCES$ZipCode )<5], sep="")
    ReducedCES$ZipCode[nchar(ReducedCES$ZipCode )<5] <-  paste("0", ReducedCES$ZipCode[nchar(ReducedCES$ZipCode )<5], sep="")
    ReducedCES$ZipCode[nchar(ReducedCES$ZipCode )<5] <-  paste("0", ReducedCES$ZipCode[nchar(ReducedCES$ZipCode )<5], sep="")
    ReducedCES$CD <- CES$cdid115
    ReducedCES$StateZip <- paste(ReducedCES$State,ReducedCES$ZipCode, sep="-")
    ReducedCES$StateCD <- paste(ReducedCES$State,ReducedCES$CD, sep="-")

  # Gender
  ReducedCES$Female <- as.numeric(CES$gender)
  ReducedCES$Female[ReducedCES$Female==1] <- 0
  ReducedCES$Female[ReducedCES$Female==2] <- 1
  ReducedCES$Female[ReducedCES$Female>1] <- NA
  
  # Ideology
  ReducedCES$Ideology <- as.numeric(as.numeric(CES$CC18_334A))
    ReducedCES$Ideology[ReducedCES$Ideology>7] <- NA

  # Party
  ReducedCES$pid3 <- as.numeric(CES$pid3)
    ReducedCES$Republican <- 0
    ReducedCES$Republican[ReducedCES$pid3=="2"] <- 1
    ReducedCES$Independent <- 0
    ReducedCES$Independent[ReducedCES$pid3=="3"] <- 1
  ReducedCES$pid7 <- as.character(CES$pid7)
    ReducedCES$StrongPartisan <- 0
    ReducedCES$StrongPartisan[ReducedCES$pid7=="7"] <- 1
    ReducedCES$StrongPartisan[ReducedCES$pid7=="1"] <- 1
    ReducedCES$pid7 <- NULL
  

  # Federal Knowledge
    CES$CC18_310b <- as.numeric(CES$CC18_310b)
      CES$CC18_310b[CES$CC18_310b==2] <- "Republican"
      CES$CC18_310b[CES$CC18_310b==3] <- "Democratic"
      CES$CC18_310b[CES$CC18_310b==4] <- "Independent"
      CES$CC18_310b[CES$CC18_310b==5] <- "Missing"
      CES$CC18_310b[CES$CC18_310b==1] <- "Never Heard Of"
      
    CES$CurrentSen1Party <- as.character(CES$CurrentSen1Party)
    
    CES$CC18_310c <- as.numeric(CES$CC18_310c)
      CES$CC18_310c[CES$CC18_310c==2] <- "Republican"
      CES$CC18_310c[CES$CC18_310c==3] <- "Democratic"
      CES$CC18_310c[CES$CC18_310c==4] <- "Independent"
      CES$CC18_310c[CES$CC18_310c==5] <- "Missing"
      CES$CC18_310c[CES$CC18_310c==1] <- "Never Heard Of"
    CES$CurrentSen2Party <- as.character(CES$CurrentSen2Party)
    
    CES$CC18_310d <- as.numeric(CES$CC18_310d)
      CES$CC18_310d[CES$CC18_310d==2] <- "Republican"
      CES$CC18_310d[CES$CC18_310d==3] <- "Democratic"
      CES$CC18_310d[CES$CC18_310d==4] <- "Independent"
      CES$CC18_310d[CES$CC18_310d==5] <- "Missing"
      CES$CC18_310d[CES$CC18_310d==1] <- "Never Heard Of"
    CES$CurrentHouseParty <- as.character(CES$CurrentHouseParty)
  
  # Determine if the respondent was correct, used for knowledge index later  
  CES$Correct_SenatorOne <- 0
    CES$Correct_SenatorOne[CES$CC18_310b==CES$CurrentSen1Party] <- 1 
    CES$Correct_SenatorTwo <- 0
    CES$Correct_SenatorTwo[CES$CC18_310c==CES$CurrentSen2Party] <- 1
    CES$Correct_USHouseRep <- 0
    CES$Correct_USHouseRep[CES$CC18_310d==CES$CurrentHouseParty] <- 1
  
  ReducedCES$Correct_SenatorOne <- CES$Correct_SenatorOne
  ReducedCES$Correct_SenatorTwo <- CES$Correct_SenatorTwo
  ReducedCES$Correct_USHouseRep <- CES$Correct_USHouseRep
  
  ###
  # Knowledge of Partisan Control
  ###
  
  ReducedCES$Belief_USSenate <- as.numeric(CES$CC18_309b)
  ReducedCES$Belief_USHouse <- as.numeric(CES$CC18_309a)
  ReducedCES$Belief_Governor <- as.numeric(CES$CC18_310a)
  ReducedCES$Belief_StateSenate <- as.numeric(CES$CC18_309c)
  ReducedCES$Belief_StateHouse <- as.numeric(CES$CC18_309d)

  # 2018 Factual Compositions
  ReducedCES$True_USSenate <- 1
  ReducedCES$True_USHouse <- 1
  ReducedCES$True_Governor <- as.character(CES$CurrentGovParty)
  ReducedCES$True_Governor[ReducedCES$True_Governor=="Democratic"] <- 3
  ReducedCES$True_Governor[ReducedCES$True_Governor=="Republican"] <- 2
  ReducedCES$True_Governor[ReducedCES$True_Governor=="Independent"] <- 4
  ReducedCES$True_StateSenate <- CES$True_StateSenate_2018
  ReducedCES$SS_DemShare <- CES$SS_DemShare_2018
  ReducedCES$True_StateHouse <- CES$True_StateHouse_2018
  ReducedCES$SH_DemShare <- CES$SH_DemShare_2018
  
  # Determine if the respondent was correct, used for knowledge index later 
  ReducedCES$Correct_Governor <- 0
  ReducedCES$Correct_Governor[ReducedCES$True_Governor==ReducedCES$Belief_Governor] <- 1
  ReducedCES$Correct_Governor[is.na(ReducedCES$Belief_Governor)] <- NA
  ReducedCES$Correct_USSenate <- 0
  ReducedCES$Correct_USSenate[ReducedCES$True_USSenate==ReducedCES$Belief_USSenate] <- 1
  ReducedCES$Correct_USSenate[is.na(ReducedCES$Belief_USSenate)] <- NA
  ReducedCES$Correct_USHouse <- 0
  ReducedCES$Correct_USHouse[ReducedCES$True_USHouse==ReducedCES$Belief_USHouse] <- 1
  ReducedCES$Correct_USHouse[is.na(ReducedCES$Belief_USHouse)] <- NA
  ReducedCES$Correct_StateSenate <- 0
  ReducedCES$Correct_StateSenate[ReducedCES$True_StateSenate==ReducedCES$Belief_StateSenate] <- 1
  ReducedCES$Correct_StateSenate[is.na(ReducedCES$Belief_StateSenate)] <- NA
  ReducedCES$Correct_StateHouse <- 0
  ReducedCES$Correct_StateHouse[ReducedCES$True_StateHouse==ReducedCES$Belief_StateHouse] <- 1
  ReducedCES$Correct_StateHouse[is.na(ReducedCES$Belief_StateHouse)] <- NA
  
  # Create Knowledge Indexes
  ReducedCES$FederalKnowledge <- ReducedCES$Correct_SenatorOne + ReducedCES$Correct_SenatorTwo + ReducedCES$Correct_USHouseRep + ReducedCES$Correct_USSenate + ReducedCES$Correct_USHouse
  ReducedCES$StateKnowledge <- ReducedCES$Correct_StateHouse + ReducedCES$Correct_StateSenate + ReducedCES$Correct_Governor
  
  
  # Religiosity
  ReducedCES$religousity_char <- CES$pew_churatd
  ReducedCES$Religiosity <- NA
  ReducedCES$Religiosity[ReducedCES$religousity_char==6] <- 1
  ReducedCES$Religiosity[ReducedCES$religousity_char==5] <- 2
  ReducedCES$Religiosity[ReducedCES$religousity_char==4] <- 3
  ReducedCES$Religiosity[ReducedCES$religousity_char==3] <- 4
  ReducedCES$Religiosity[ReducedCES$religousity_char==2] <- 5
  ReducedCES$Religiosity[ReducedCES$religousity_char==1] <- 6
  ReducedCES$religousity_char <- NULL
  
  # College Degree
  ReducedCES$educ <- CES$educ
  ReducedCES$CollegeDegree <- 0
  ReducedCES$CollegeDegree[ReducedCES$educ==5] <- 1
  ReducedCES$CollegeDegree[ReducedCES$educ==6] <- 1
  ReducedCES$educ <- NULL
  
  # Race
  ReducedCES$Nonwhite <- as.numeric(CES$race)
  ReducedCES$Nonwhite[ReducedCES$Nonwhite==1] <- 0
  ReducedCES$Nonwhite[ReducedCES$Nonwhite!=0] <- 1
  
  ReducedCES$White <- as.numeric(CES$race)
  ReducedCES$White[ReducedCES$White!=1] <- 0
  ReducedCES$White[ReducedCES$White!=0] <- 1
  
  CES$Black <- 0
  CES$Black[CES$race==2] <- 1
  ReducedCES$Black <- CES$Black

  # Age
  ReducedCES$Age <- 2019 - CES$birthyr
  
  # Employed
  ReducedCES$employed_char <- CES$employ
  ReducedCES$Employed <- 0
  ReducedCES$Employed[ReducedCES$employed_char==1] <- 1
  ReducedCES$Employed[ReducedCES$employed_char==2] <- 1
  
  ReducedCES$employed_char <- NULL
  
  
  ####
  # Additions for Knowledge Analysis
  ####
  
  # Overall Income Category
  ReducedCES$Income <- as.numeric(CES$faminc)
  ReducedCES$Income[ReducedCES$Income==97] <- NA
  ReducedCES$Income[ReducedCES$Income==98] <- NA
  ReducedCES$Income[ReducedCES$Income==99] <- NA
  ReducedCES$Income[ReducedCES$Income>=12] <- 12

  
  ReducedCES$Income <- as.numeric(ReducedCES$Income)
  
  # Overall Education Category
  ReducedCES$Education <- as.numeric(CES$educ)
  
  # Education Sub Categories
  ReducedCES$Educ_NoHS <- 0
  ReducedCES$Educ_NoHS[ReducedCES$Education==1] <- 1
  
  ReducedCES$Educ_HS <- 0
  ReducedCES$Educ_HS[ReducedCES$Education==2] <- 1
  
  ReducedCES$Educ_SomeCollege <- 0
  ReducedCES$Educ_SomeCollege[ReducedCES$Education==3] <- 1
  
  ReducedCES$Educ_TwoYrCollege <- 0
  ReducedCES$Educ_TwoYrCollege[ReducedCES$Education==4] <- 1
  
  ReducedCES$Educ_FourYrCollege <- 0
  ReducedCES$Educ_FourYrCollege[ReducedCES$Education==5] <- 1
  
  ReducedCES$Educ_PostGrad <- 0
  ReducedCES$Educ_PostGrad[ReducedCES$Education==6] <- 1
  
  
  ###
  # Racial Resentment (RR) Measures
  ###
  
  # Coding Variables so higher values are more resentful / less sympathetic to racial problems
  
  # White people in the U.S. have certain advantages because of the color of their skin.
  ReducedCES$RR_SkinAdvantages <- as.numeric(CES$CC18_422a)
  ReducedCES$RR_SkinAdvantages[ReducedCES$RR_SkinAdvantages==1] <- -2
  ReducedCES$RR_SkinAdvantages[ReducedCES$RR_SkinAdvantages==2] <- -1
  ReducedCES$RR_SkinAdvantages[ReducedCES$RR_SkinAdvantages==3] <- 0
  ReducedCES$RR_SkinAdvantages[ReducedCES$RR_SkinAdvantages==4] <- 1
  ReducedCES$RR_SkinAdvantages[ReducedCES$RR_SkinAdvantages==5] <- 2
  ReducedCES$RR_SkinAdvantages <- as.numeric(ReducedCES$RR_SkinAdvantages) + 2
  
  # Racial Problems in the US are rare, isolated situations
  ReducedCES$RR_RacialProblemsRare <- as.numeric(CES$CC18_422b)
  ReducedCES$RR_RacialProblemsRare[ReducedCES$RR_RacialProblemsRare==1] <- -2
  ReducedCES$RR_RacialProblemsRare[ReducedCES$RR_RacialProblemsRare==2] <- -1
  ReducedCES$RR_RacialProblemsRare[ReducedCES$RR_RacialProblemsRare==3] <- 0
  ReducedCES$RR_RacialProblemsRare[ReducedCES$RR_RacialProblemsRare==4] <- 1
  ReducedCES$RR_RacialProblemsRare[ReducedCES$RR_RacialProblemsRare==5] <- 2
  ReducedCES$RR_RacialProblemsRare <- as.numeric(ReducedCES$RR_RacialProblemsRare) + 2
  
  # Irish, Italians, Jewish and many other minorities overcame prejudice and worked their way up. Blacks should do the same without any special favors.
  ReducedCES$RR_SpecialFavors <- as.numeric(CES$CC18_422e)
  ReducedCES$RR_SpecialFavors[ReducedCES$RR_SpecialFavors==1] <- 2
  ReducedCES$RR_SpecialFavors[ReducedCES$RR_SpecialFavors==2] <- 1
  ReducedCES$RR_SpecialFavors[ReducedCES$RR_SpecialFavors==3] <- 0
  ReducedCES$RR_SpecialFavors[ReducedCES$RR_SpecialFavors==4] <- -1
  ReducedCES$RR_SpecialFavors[ReducedCES$RR_SpecialFavors==5] <- -2
  ReducedCES$RR_SpecialFavors <- as.numeric(ReducedCES$RR_SpecialFavors) + 2
  
  # Generations of slavery and discrimination have created conditions that make it difficult for blacks to work their way out of the lower class.
  ReducedCES$RR_Slavery <- as.numeric(CES$CC18_422f)
  ReducedCES$RR_Slavery[ReducedCES$RR_Slavery==1] <- -2
  ReducedCES$RR_Slavery[ReducedCES$RR_Slavery==2] <- -1
  ReducedCES$RR_Slavery[ReducedCES$RR_Slavery==3] <- 0
  ReducedCES$RR_Slavery[ReducedCES$RR_Slavery==4] <- 1
  ReducedCES$RR_Slavery[ReducedCES$RR_Slavery==5] <- 2
  ReducedCES$RR_Slavery <- as.numeric(ReducedCES$RR_Slavery) + 2
  
  # Over the past few years, blacks have gotten less than they deserve.
  ReducedCES$RR_LessDeserve <- as.numeric(CES$CC18_422g)
  ReducedCES$RR_LessDeserve[ReducedCES$RR_LessDeserve==1] <- -2
  ReducedCES$RR_LessDeserve[ReducedCES$RR_LessDeserve==2] <- -1
  ReducedCES$RR_LessDeserve[ReducedCES$RR_LessDeserve==3] <- 0
  ReducedCES$RR_LessDeserve[ReducedCES$RR_LessDeserve==4] <- 1
  ReducedCES$RR_LessDeserve[ReducedCES$RR_LessDeserve==5] <- 2
  ReducedCES$RR_LessDeserve <- as.numeric(ReducedCES$RR_LessDeserve) + 2
  
  # It’s really a matter of some people not trying hard enough, if blacks would only try harder they could be just as well off as whites.
  ReducedCES$RR_TryHarder <- as.numeric(CES$CC18_422h)
  ReducedCES$RR_TryHarder[ReducedCES$RR_TryHarder==1] <- 2
  ReducedCES$RR_TryHarder[ReducedCES$RR_TryHarder==2] <- 1
  ReducedCES$RR_TryHarder[ReducedCES$RR_TryHarder==3] <- 0
  ReducedCES$RR_TryHarder[ReducedCES$RR_TryHarder==4] <- -1
  ReducedCES$RR_TryHarder[ReducedCES$RR_TryHarder==5] <- -2
  ReducedCES$RR_TryHarder <- as.numeric(ReducedCES$RR_TryHarder) + 2
  
  # Racial Resentment ~ Tesler
  # https://www.press.uchicago.edu/books/tesler/tesler_post-racial_appendix.pdf
  ReducedCES$RR_Tesler <- as.numeric(ReducedCES$RR_SpecialFavors) + as.numeric(ReducedCES$RR_Slavery)
  ReducedCES$RR_Tesler <- (ReducedCES$RR_Tesler)/8

  ReducedCES$RR_Kinder <- as.numeric(ReducedCES$RR_SpecialFavors) + as.numeric(ReducedCES$RR_LessDeserve) + as.numeric(ReducedCES$RR_TryHarder) + as.numeric(ReducedCES$RR_Slavery)

  ReducedCES$RR_Kinder <- (ReducedCES$RR_Kinder)/16

  ####
  # Sexism Measures
  ####
  
  # When women lose to men in a fair competition, they typically complain about being discriminated against
  ReducedCES$Fem_Complain <- as.numeric(CES$CC18_422c)
  ReducedCES$Fem_Complain[ReducedCES$Fem_Complain==1] <- 2
  ReducedCES$Fem_Complain[ReducedCES$Fem_Complain==2] <- 1
  ReducedCES$Fem_Complain[ReducedCES$Fem_Complain==3] <- 0
  ReducedCES$Fem_Complain[ReducedCES$Fem_Complain==4] <- -1
  ReducedCES$Fem_Complain[ReducedCES$Fem_Complain==5] <- -2
  ReducedCES$Fem_Complain <- as.numeric(ReducedCES$Fem_Complain)
  
  unique(ReducedCES$Fem_Complain)
  
  # Feminists are making entirely reasonable demands of men
  ReducedCES$Fem_Demands <- as.numeric(CES$CC18_422d)
  ReducedCES$Fem_Demands[ReducedCES$Fem_Demands==1] <- -2
  ReducedCES$Fem_Demands[ReducedCES$Fem_Demands==2] <- -1
  ReducedCES$Fem_Demands[ReducedCES$Fem_Demands==3] <- 0
  ReducedCES$Fem_Demands[ReducedCES$Fem_Demands==4] <- 1
  ReducedCES$Fem_Demands[ReducedCES$Fem_Demands==5] <- 2
  ReducedCES$Fem_Demands <- as.numeric(ReducedCES$Fem_Demands)
  
  unique(ReducedCES$Fem_Demands)
  
  ReducedCES$Fem_Tesler <- as.numeric(ReducedCES$Fem_Complain) + as.numeric(ReducedCES$Fem_Demands)
  ReducedCES$Fem_Tesler <- (ReducedCES$Fem_Tesler+4)/8 
  
####
# Get Stauffer Relevant Variables from SLU CES Module
####
  
  ReducedSLU <- data.frame(matrix(NA, nrow(SLUModule),0))
  ReducedSLU$caseid <- SLUModule$caseid
  ReducedSLU$teamweight <- SLUModule$teamweight
  ReducedSLU$WaveOneDate <- SLUModule$starttime
  
  # Legislators Demographics
  ReducedSLU$Belief_WomenCongress <- SLUModule$SLU449
  ReducedSLU$Belief_WomenCongress[ReducedSLU$Belief_WomenCongress==999] <- NA
  ReducedSLU$Belief_WomenStateLeg <- SLUModule$SLU450
  ReducedSLU$Belief_WomenStateLeg[ReducedSLU$Belief_WomenStateLeg==999] <- NA
  ReducedSLU$Belief_BlackCongress <- SLUModule$SLU451
  ReducedSLU$Belief_BlackCongress[ReducedSLU$Belief_BlackCongress==999] <- NA
  ReducedSLU$Belief_BlackStateLeg <- SLUModule$SLU452
  ReducedSLU$Belief_BlackStateLeg[ReducedSLU$Belief_BlackStateLeg==999] <- NA
  
  ####
  # Efficacy
  ####
  
  # Functions to Record Variables
  RecodeOnetoFive <- function(CurrentVariable)
      {
        TempDataFrame <- data.frame(matrix(NA, length(CurrentVariable),0))
        TempDataFrame$CurrentCoding <- CurrentVariable
        TempDataFrame$NewCoding <- NA
        TempDataFrame$NewCoding[TempDataFrame$CurrentCoding==1] <- 2
        TempDataFrame$NewCoding[TempDataFrame$CurrentCoding==2] <- 1
        TempDataFrame$NewCoding[TempDataFrame$CurrentCoding==3] <- 0
        TempDataFrame$NewCoding[TempDataFrame$CurrentCoding==4] <- -1
        TempDataFrame$NewCoding[TempDataFrame$CurrentCoding==5] <- -2
        
        return(TempDataFrame$NewCoding)
      }
  
  ReducedSLU$RepPeopleLikeMe_Congress <- RecodeOnetoFive(SLUModule$SLU316)
  ReducedSLU$RepPeopleLikeMe_StateLeg <- RecodeOnetoFive(SLUModule$SLU312)
  
  ReducedSLU$Contact_MakeDifference_Congress <- RecodeOnetoFive(SLUModule$SLU332)
  ReducedSLU$Contact_MakeDifference_StateLeg <- RecodeOnetoFive(SLUModule$SLU331)
  
  ReducedSLU$ExternalEfficacy_Congress <- ((ReducedSLU$Contact_MakeDifference_Congress+2)+(ReducedSLU$RepPeopleLikeMe_Congress+2))/8
  
  ReducedSLU$Trust_StateLeg_Pre <- RecodeOnetoFive(SLUModule$SLU322)
  ReducedSLU$Trust_Congress_Pre <- RecodeOnetoFive(SLUModule$SLU324)
  
  ###
  # Combine Data
  ###
  
  nrow(ReducedSLU)
  SLUCES <- merge(ReducedSLU, ReducedCES, by.x="caseid", by.y="case_id", all.x=TRUE)
  nrow(SLUCES)
  SLUCES <- merge(SLUCES, MoCRace, by.x="StateCD", by.y="StateCD", all.x=TRUE)
  nrow(SLUCES)
  SLUCES <- merge(SLUCES, DistancetoCapital, by.x="StateZip", by.y="StateZip", all.x=TRUE)
  nrow(SLUCES)
  SLUCES <- merge(SLUCES, StateLevelVariables, by.x="State", by.y="State", all.x=TRUE)
  nrow(SLUCES)
  
  ###
  # Add Congruence Variable
  ###
  
  SLUCES$UnifiedLeg_Check <- SLUCES$True_StateSenate*SLUCES$True_StateHouse
  SLUCES$Congruence <- 0
  SLUCES$Congruence[SLUCES$UnifiedLeg_Check==1 & SLUCES$pid3==2] <- 1
  SLUCES$Congruence[SLUCES$UnifiedLeg_Check==4 & SLUCES$pid3==1] <- 1
  SLUCES$UnifiedLef_Check <- NULL
  SLUCES$True_StateHouse <- NULL
  SLUCES$True_StateSenate <- NULL
  SLUCES$pid3 <- NULL
  
  
  ###
  # Add Overestimation Data for Knowledge Analyses
  ###
  
  SLUCES$OverEstimate_WomenCongress_Size <- SLUCES$Belief_WomenCongress-SLUCES$True_WomenCongress
  SLUCES$OverEstimate_WomenStateLeg_Size <- SLUCES$Belief_WomenStateLeg-SLUCES$True_WomenStateLeg
  SLUCES$OverEstimate_BlackCongress_Size <- SLUCES$Belief_BlackCongress-SLUCES$True_BlackCongress
  SLUCES$OverEstimate_BlackStateLeg_Size <- SLUCES$Belief_BlackStateLeg-SLUCES$True_BlackStateLeg
  
  SLUCES$OverEstimate_WomenCongress <- NA
  SLUCES$OverEstimate_WomenCongress[SLUCES$OverEstimate_WomenCongress_Size<=0] <- 0
  SLUCES$OverEstimate_WomenCongress[SLUCES$OverEstimate_WomenCongress_Size>0] <- 1
  
  SLUCES$OverEstimate_WomenStateLeg <- NA
  SLUCES$OverEstimate_WomenStateLeg[SLUCES$OverEstimate_WomenStateLeg_Size<=0] <- 0
  SLUCES$OverEstimate_WomenStateLeg[SLUCES$OverEstimate_WomenStateLeg_Size>0] <- 1
  
  SLUCES$OverEstimate_BlackCongress <- NA
  SLUCES$OverEstimate_BlackCongress[SLUCES$OverEstimate_BlackCongress_Size<=0] <- 0
  SLUCES$OverEstimate_BlackCongress[SLUCES$OverEstimate_BlackCongress_Size>0] <- 1 
  
  SLUCES$OverEstimate_BlackStateLeg <- NA
  SLUCES$OverEstimate_BlackStateLeg[SLUCES$OverEstimate_BlackStateLeg_Size<=0] <- 0
  SLUCES$OverEstimate_BlackStateLeg[SLUCES$OverEstimate_BlackStateLeg_Size>0] <- 1 
  
  ###
  # Add Correct Data for Knowledge Analyses
  ###
  
  SLUCES$WomenCongress_Correct <- NA
  SLUCES$WomenCongress_Error <- abs(SLUCES$Belief_WomenCongress-SLUCES$True_WomenCongress)
  SLUCES$WomenCongress_Correct[SLUCES$WomenCongress_Error<=5] <- 1
  SLUCES$WomenCongress_Correct[SLUCES$WomenCongress_Error>5] <- 0
  
  SLUCES$WomenStateLeg_Correct <- NA
  SLUCES$WomenStateLeg_Error <- abs(SLUCES$Belief_WomenStateLeg-SLUCES$True_WomenStateLeg)
  SLUCES$WomenStateLeg_Correct[SLUCES$WomenStateLeg_Error<=5] <- 1
  SLUCES$WomenStateLeg_Correct[SLUCES$WomenStateLeg_Error>5] <- 0
  
  SLUCES$BlackCongress_Correct <- NA
  SLUCES$BlackCongress_Error <- abs(SLUCES$Belief_BlackCongress-SLUCES$True_BlackCongress)
  SLUCES$BlackCongress_Correct[SLUCES$BlackCongress_Error<=5] <- 1
  SLUCES$BlackCongress_Correct[SLUCES$BlackCongress_Error>5] <- 0
  
  SLUCES$BlackStateLeg_Correct <- NA
  SLUCES$BlackStateLeg_Error <- abs(SLUCES$Belief_BlackStateLeg-SLUCES$True_BlackStateLeg)
  SLUCES$BlackStateLeg_Correct[SLUCES$BlackStateLeg_Error<=5] <- 1
  SLUCES$BlackStateLeg_Correct[SLUCES$BlackStateLeg_Error>5] <- 0
  
  # Add Labels for STATA Tables
  
  attr(SLUCES$Belief_WomenCongress, "label") <- "Estimate % Women"
  attr(SLUCES$Belief_BlackCongress, "label") <- "Estimate % Black"
  attr(SLUCES$Belief_WomenStateLeg, "label") <- "Estimate % women"
  attr(SLUCES$Belief_BlackStateLeg, "label") <- "Estimate % black"
  attr(SLUCES$True_WomenStateLeg, "label") <- "Women in state leg."
  attr(SLUCES$True_BlackStateLeg, "label") <- "Black in State Leg"
  
  attr(SLUCES$Female, "label") <- "Female"
  attr(SLUCES$Black, "label") <- "Black"
  attr(SLUCES$CollegeDegree, "label") <- "Education"
  attr(SLUCES$White, "label") <- "White"
  attr(SLUCES$Age, "label") <- "Age"
  attr(SLUCES$Employed, "label") <- "Employed"
  attr(SLUCES$Religiosity, "label") <- "Religiosity"
  attr(SLUCES$LogDistancefromCapital, "label") <- "Distance to Capital (Logged)"
  attr(SLUCES$State_Ideology, "label") <- "State gov. liberalism."
  
  attr(SLUCES$StateKnowledge, "label") <- "Political knowledge (State)"
  attr(SLUCES$FederalKnowledge, "label") <- "Political Knowledge (Fed)"
  
  attr(SLUCES$Ideology, "label") <- "Ideology (lib to cons.)"
  attr(SLUCES$Independent, "label") <- "Independent"
  attr(SLUCES$Republican, "label") <- "Republican"
  attr(SLUCES$StrongPartisan, "label") <- "Strong Partisan"
  
  attr(SLUCES$USSenator_Female, "label") <- "Woman sen."
  attr(SLUCES$MoC_Female, "label") <- "Woman rep."
  attr(SLUCES$USSenator_Black, "label") <- "Black sen."
  attr(SLUCES$MoC_Black, "label") <- "Black rep."
  
  attr(SLUCES$Ideology, "label") <- "Ideology (lib. to cons.)"
  attr(SLUCES$State_Ideology, "label") <- "State gov. liberalism"
  attr(SLUCES$Congruence, "label") <- "Congruence"
  
  attr(SLUCES$Fem_Tesler, "label") <- "Hostile Sexism"
  attr(SLUCES$RR_Tesler, "label") <- "Racial Resentment (Tesler)"
  attr(SLUCES$RR_Kinder, "label") <- "Racial Resentment (Kinder & Sanders)"
  
  write_dta(SLUCES, "Data/Formatted Data/Clark-Rogers-RegressionData.dta")

  ###
  # The below takes the Stauffer Replication files and renames/relabels the variables sor they are consistent across the datasets.
  # This is useful so that tables that include analyses from both datasets can be easily merged using esttab.
  ###
  
  
  # Remake Variables Stauffer Data for Consistency Between Datasets
    # This will make creating tables easier by having common names
    StaufferCongress$State <- StaufferCongress$state
    StaufferCongress$Belief_WomenCongress <- StaufferCongress$r_estimate
    StaufferCongress$Female <- StaufferCongress$gender_r
    StaufferCongress$Ideology <- StaufferCongress$ideo7
    StaufferCongress$Independent <- NA
      StaufferCongress$Independent[StaufferCongress$party==1] <- 0
      StaufferCongress$Independent[StaufferCongress$party==2] <- 1
      StaufferCongress$Independent[StaufferCongress$party==3] <- 0
    StaufferCongress$Republican <- NA
      StaufferCongress$Republican[StaufferCongress$party==1] <- 0
      StaufferCongress$Republican[StaufferCongress$party==2] <- 0
      StaufferCongress$Republican[StaufferCongress$party==3] <- 1
    StaufferCongress$StrongPartisan <- StaufferCongress$strong_partisan
    StaufferCongress$FederalKnowledge <- StaufferCongress$know_fed
    StaufferCongress$Religiosity <- StaufferCongress$religiosity
    StaufferCongress$CollegeDegree <- StaufferCongress$coll
    StaufferCongress$White <- StaufferCongress$white
    StaufferCongress$Age <- StaufferCongress$age
    StaufferCongress$Employed <- StaufferCongress$employed
    StaufferCongress$USSenator_Female <- StaufferCongress$woman_sen
    StaufferCongress$MoC_Female <- StaufferCongress$woman_rep
    StaufferCongress$True_WomenStateLeg <- StaufferCongress$stateleg


    attr(StaufferCongress$Belief_WomenCongress, "label") <- "Estimate % Women"
    attr(StaufferCongress$Female, "label") <- "Female"
    attr(StaufferCongress$CollegeDegree, "label") <- "Education"
    attr(StaufferCongress$White, "label") <- "White"
    attr(StaufferCongress$Age, "label") <- "Age"
    attr(StaufferCongress$Employed, "label") <- "Employed"
    attr(StaufferCongress$Religiosity, "label") <- "Religiosity"
    attr(StaufferCongress$FederalKnowledge, "label") <- "Political Knowledge (Fed)"
    attr(StaufferCongress$Ideology, "label") <- "Ideology (lib to cons.)"
    attr(StaufferCongress$Independent, "label") <- "Independent"
    attr(StaufferCongress$Republican, "label") <- "Republican"
    attr(StaufferCongress$StrongPartisan, "label") <- "Strong Partisan"
    attr(StaufferCongress$USSenator_Female, "label") <- "Woman sen."
    attr(StaufferCongress$MoC_Female, "label") <- "Woman rep."
  write_dta(StaufferCongress, "Data/Formatted Data/Stauffer-Congress-RegressionData.dta")
    
  ###
  # The below takes the Stauffer Replication files and renames/relabels the variables sor they are consistent across the datasets.
  # This is useful so that tables that include analyses from both datasets can be easily merged using esttab.
  ###
  
  StaufferStateLeg$Belief_WomenStateLeg <- StaufferStateLeg$state_guess
  StaufferStateLeg$Female <- StaufferStateLeg$gender_r
  StaufferStateLeg$Ideology <- StaufferStateLeg$ideo7
  StaufferStateLeg$State_Ideology <- StaufferStateLeg$state_ideo01
  StaufferStateLeg$Congruence <- StaufferStateLeg$congruence
  StaufferStateLeg$Independent <- NA
  StaufferStateLeg$Independent[StaufferStateLeg$party==1] <- 0
  StaufferStateLeg$Independent[StaufferStateLeg$party==2] <- 1
  StaufferStateLeg$Independent[StaufferStateLeg$party==3] <- 0
  StaufferStateLeg$Republican <- NA
  StaufferStateLeg$Republican[StaufferStateLeg$party==1] <- 0
  StaufferStateLeg$Republican[StaufferStateLeg$party==2] <- 0
  StaufferStateLeg$Republican[StaufferStateLeg$party==3] <- 1
  StaufferStateLeg$StrongPartisan <- StaufferStateLeg$strong_partisan
  StaufferStateLeg$StateKnowledge <- StaufferStateLeg$know_state
  StaufferStateLeg$Religiosity <- StaufferStateLeg$religiosity
  StaufferStateLeg$CollegeDegree <- StaufferStateLeg$coll
  StaufferStateLeg$White <- StaufferStateLeg$white
  StaufferStateLeg$Age <- StaufferStateLeg$age
  StaufferStateLeg$Employed <- StaufferStateLeg$employed
  StaufferStateLeg$USSenator_Female <- StaufferStateLeg$woman_sen
  StaufferStateLeg$MoC_Female <- StaufferStateLeg$woman_rep
  StaufferStateLeg$True_WomenStateLeg <- StaufferStateLeg$state
  StaufferStateLeg$State <- StaufferStateLeg$StateAbbr
  StaufferStateLeg$RepPeopleLikeMe_StateLeg <- StaufferStateLeg$state_responsive
  
  attr(StaufferStateLeg$Belief_WomenStateLeg, "label") <- "Estimate % Women"
  attr(StaufferStateLeg$True_WomenStateLeg, "label") <- "Women in State Leg"
  attr(StaufferStateLeg$Female, "label") <- "Female"
  attr(StaufferStateLeg$CollegeDegree, "label") <- "Education"
  attr(StaufferStateLeg$White, "label") <- "White"
  attr(StaufferStateLeg$Age, "label") <- "Age"
  attr(StaufferStateLeg$Employed, "label") <- "Employed"
  attr(StaufferStateLeg$Religiosity, "label") <- "Religiosity"
  attr(StaufferStateLeg$StateKnowledge, "label") <- "Political Knowledge (State)"
  attr(StaufferStateLeg$Ideology, "label") <- "Ideology (lib to cons.)"
  attr(StaufferStateLeg$State_Ideology, "label") <- "State gov. liberalism."
  
  attr(StaufferStateLeg$Independent, "label") <- "Independent"
  attr(StaufferStateLeg$Republican, "label") <- "Republican"
  attr(StaufferStateLeg$StrongPartisan, "label") <- "Strong Partisan"
  attr(StaufferStateLeg$USSenator_Female, "label") <- "Woman sen."
  attr(StaufferStateLeg$MoC_Female, "label") <- "Woman rep."

  write_dta(StaufferStateLeg, "Data/Formatted Data/Stauffer-StateLeg-RegressionData.dta")
  
  